/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package three2learn.Controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;
import three2learn.Model.Admin;
import three2learn.service.HibernateUtil;

/**
 *
 * @author Kevin
 */
public class AdminWijzigController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = session.beginTransaction();

        Admin formAdmin = getAdminFromRequest(request);
        
        session.save(formAdmin);
        transaction.commit();
        
        String address = "/createAdmin.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

    private Admin getAdminFromRequest(HttpServletRequest request) {
        Admin a = new Admin();

        if (request.getParameter("id") != null && !request.getParameter("id").isEmpty()) {
            a.setAdminNumber(Long.parseLong(request.getParameter("id")));
        }
        if (request.getParameter("firstName") != null) {
            a.setFirstName(request.getParameter("firstName"));
        }
        if (request.getParameter("lastName") != null) {
            a.setLastName(request.getParameter("lastName"));
        }
        if (request.getParameter("eMail") != null) {
            a.seteMail(request.getParameter("eMail"));
        }
        if (request.getParameter("Passw") != null) {
            a.setPassword(request.getParameter("Passw"));
        }
        return a;
    }
}
